package com.ruoyi.system.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.util.ListUtils;
import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.system.domain.InvoiceRecords;
import com.ruoyi.system.domain.ProvideRecords;
import com.ruoyi.system.domain.ProvideWare;
import com.ruoyi.system.service.impl.InvoiceRecordsServiceImpl;
import com.ruoyi.system.service.impl.ProvideRecordsServiceImpl;
import com.ruoyi.system.service.impl.ProvideWareServiceImpl;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Slf4j
public class NoModelDataListener5 extends AnalysisEventListener<Map<Integer, String>> {




    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;
    private List<Map<Integer, String>> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    private ProvideRecordsServiceImpl provideRecordsService;
    private SysDeptServiceImpl sysDeptService;
    private ProvideWareServiceImpl provideWareService;
    private String time;

    public NoModelDataListener5(ProvideRecordsServiceImpl provideRecordsService,SysDeptServiceImpl sysDeptService,String time,ProvideWareServiceImpl provideWareService){
        this.provideRecordsService = provideRecordsService;
        this.sysDeptService = sysDeptService;
        this.time = time;
        this.provideWareService = provideWareService;
    }

    @Override
    @SneakyThrows
    public void invoke(Map<Integer, String> data, AnalysisContext context){
        log.info("解析到一条数据:{}", JSON.toJSONString(data));
        cachedDataList.add(data);
        if (cachedDataList.size() >= BATCH_COUNT) {
            saveData(cachedDataList);
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
        }
    }

    @Override
    @SneakyThrows
    public void doAfterAllAnalysed(AnalysisContext context){
        saveData(cachedDataList);
        log.info("所有数据解析完成！");
    }

    /**
     * 存储配件库数据
     */
    private void saveData(List<Map<Integer, String>> cachedDataList) throws IOException{
//        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (Map<Integer,String> map:cachedDataList) {
//                log.info(map.toString());
                ProvideRecords provideRecords = new ProvideRecords();
                //判断仓库编码是否存在
                String wareHouseCode = map.get(0);
                ProvideWare provideWare = new ProvideWare();
                provideWare.setWareCode(wareHouseCode);
                List<ProvideWare> provideWares = provideWareService.selectProvideWareList(provideWare);
                if(provideWares!=null&&provideWares.size()>0){
                    provideRecords.setWarehouseCode(map.get(0));
                }else {
                        throw new IOException("有不存在的仓库-" + map.get(1) + " 请先添加仓库后再导入！");
                }
                provideRecords.setWarehouseCode(map.get(0));
                provideRecords.setWarehouseName(map.get(1));
                    provideRecords.setProductCode(map.get(2));
                    provideRecords.setProductName(map.get(3));
                    provideRecords.setProductPrice(map.get(4));
                    provideRecords.setPlarnPrice(map.get(5));
                    provideRecords.setModel(map.get(6));
                    provideRecords.setSpecs(map.get(7));
                    provideRecords.setProductBrand(map.get(8));
                    String provide = map.get(10);
                    SysDept sysDept = sysDeptService.selectDeptByName(provide);
                    if (sysDept != null) {
                        provideRecords.setProvide(map.get(10));
                    } else {
                        throw new IOException("有不存在的供应商账号-" + provide + "请先添加账号再导入！");
                    }
                    provideRecords.setProvide(map.get(10));
                    provideRecords.setProductCount(map.get(11));
                    if(map.get(12).equals("1")){
                        provideRecords.setSettlementMode("压库");
                    }else if(map.get(12).equals("2")){
                        provideRecords.setSettlementMode("买断");
                    }
                    provideRecords.setCreateTime(time);
                    DecimalFormat df = new DecimalFormat("0.00##");
                    provideRecords.setProductMoney(df.format(Double.valueOf(map.get(5))*Double.valueOf(map.get(11))));
                    provideRecords.setStatus("0");
                    provideRecordsService.insertProvideRecords(provideRecords);
                }


        log.info("存储数据库成功！");
    }
}